<?php

namespace app\model;

use think\facade\Cache;
use think\Model;

class BocBanner extends Model
{
    protected $table = 'boc_banner';

    /**
     * 查询首页轮播图.
     */
    public function sel_banner($cate_id, $field = 'title,desc,cover,content,slider,video_url')
    {
        return self::field($field)->where(['menu_id' => $cate_id])->where(['status' => 1, 'is_deleted' => 0])->order('sort desc,create_at desc')->select()->toarray();
    }

    /**
     * 其他页面单张图片.
     */
    public function get_banner($cate_id)
    {
        $it = self::field('cate_id,url,cover,content,title,desc')->where(['cate_id' => $cate_id])->where(['status' => 1, 'is_deleted' => 0])->order('sort desc,create_at desc')->find();
//        $it = self::field('cate_id,url,cover,content,title')->where(['cate_id' => $cate_id])->where(['status' => 1, 'is_deleted' => 0])->cache('find_banner','300')->find();

        return $it ? $it->toarray() : [];
    }

    protected $key = 'banner';

    public function get_info($c, $a = '')
    {
        $key = 'banner-'.$c.'-'.$a;
        $cache_tag = 'banner';
        $info = Cache::get($key);

        if (empty($info)) {
            $Boc_menu = new BocMenu();
            $p = $Boc_menu->field('id')->where(['pid' => 0, 'key' => $c, 'status' => 1, 'is_deleted' => 0])->findOrEmpty()->toArray();

            //没有子集，使用父级banner
//            if (empty($a) || $a == 'index') {
//                $menu_id = $p['id'];
//                $info = $this->where(['menu_id' => $menu_id, 'status' => 1, 'is_deleted' => 0])->order('sort desc, create_at desc')->select()->toArray();
//            } else {
            $c = $Boc_menu->field('id')->where(['pid' => $p['id'], 'key' => $a, 'status' => 1, 'is_deleted' => 0])->findOrEmpty()->toArray();

            if (!empty($c)) {
                $info = $this->where(['menu_id' => $c['id'], 'status' => 1, 'is_deleted' => 0])->order('sort desc, create_at desc')->select()->toArray();
            }

            if (empty($info)) {
                //子集没有设置banner，使用父级banner
                $menu_id = $p['id'];
                $info = $this->where(['menu_id' => $menu_id, 'status' => 1, 'is_deleted' => 0])->order('sort desc, create_at desc')->select()->toArray();
            }
//            }

            Cache::tag($cache_tag)->set($key, $info);
        }

        return $info;
    }

    public function get_banner_list($tid)
    {
        $info = $this->where(['menu_id' => $tid, 'status' => 1, 'is_deleted' => 0])->order('sort desc, create_at desc')->select()->toArray();

        return $info;
    }
}
